package design.pattern.Lu18MementoPattern;

/**
 * @program JavaBase
 * @description: 备忘录模式 + 备忘录管理者
 * @author: zhanglu
 * @create: 2020-04-10 15:46:00
 */
public class MementoPattern02 {

    public static void main(String[] args) {
        Boy02 boy02 = new Boy02();
        boy02.setState("感觉良好");

        Caretaker caretaker = new Caretaker();
        caretaker.setMemento02(boy02.createMemento());

        boy02.print();
        boy02.changeState();
        boy02.print();

        boy02.restoreMemento(caretaker.getMemento02());
        boy02.print();

    }

}

class Boy02{

    private String state;

    public void changeState(){
        this.state = "不太好";
    }

    public void setState(String state){
        this.state = state;
    }

    public String getState(){
        return this.state;
    }

    public void print(){
        System.out.println(this.state);
    }

    public Memento02 createMemento(){
        return new Memento02(this.state);
    }

    public void restoreMemento(Memento02 memento02){
        this.setState(memento02.getState());
    }

}

class Memento02{

    private String state;

    public Memento02(String state){
        this.state = state;
    }
    public String getState(){
        return this.state;
    }

}

class Caretaker{
    private Memento02 memento02;

    public Memento02 getMemento02() {
        return memento02;
    }

    public void setMemento02(Memento02 memento02) {
        this.memento02 = memento02;
    }
}